<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>编辑个人设置-学生</title>
	<link rel="stylesheet" href="css/left_style.css">
	<link rel="stylesheet" href="css/common.css">
	<link rel="stylesheet" href="css/headers.css">
	<link rel="stylesheet" href="css/per_edit.css">
</head>
<body>
<!-- 头部信息 -->
<div class="header">
	<div class="header-logo">
		<div class="header-portrait">
<?php
$img_logo = "images/touxiang.jpg";

include_once("database.php");

if(isset($_COOKIE["stu_number"])) {

	$username = $_COOKIE["stu_number"];

	$selectstu = "select * from students where stu_number='$username'";
	$result = mysql_query($selectstu) or die("查询学生信息表头像失败：".mysql_error()."<br/>");

	$row = mysql_fetch_array($result,MYSQL_ASSOC);

	$stu_name = $row["stu_name"];
	$school = $row["school"];
	$stu_phone = $row["stu_phone"];
	$stu_email = $row["stu_email"];
	$stu_province = $row["stu_province"];
	$stu_city = $row["stu_city"];
	$stu_county = $row["stu_county"];
	$stu_sex = $row["stu_sex"];
	$name = $row["name"];
	$stuper_signature = $row["stuper_signature"];
	$img_logo = $row["stu_touxiang"];


	if($img_logo == NULL) {
		$img_logo = "images/touxiang.jpg";
	} else {
		$img_logo = $row["stu_touxiang"];
	}
}
?>
			<?php echo "<img src='{$img_logo}' alt='logo' id='logo'>";?>	
		</div>
		<div class="header-text">
<?php 
$stu_name = "你的昵称";

if(isset($_COOKIE["stu_number"])) {

	$username = $_COOKIE["stu_number"];

	$selectstu = "select stu_name from students where stu_number='$username'";
	$result = mysql_query($selectstu) or die("查询学生信息表头像失败：".mysql_error()."<br/>");

	$row = mysql_fetch_array($result,MYSQL_ASSOC);

	$stu_name = $row["stu_name"];

	if($stu_name == NULL) {
		$stu_name = "你的昵称";
	} else {
		$stu_name = $row["stu_name"];
	}
}
?>
			<?php echo "<h2>{$stu_name}</h2>";?>
<?php
if(isset($_COOKIE["stu_number"]) && $_COOKIE["stu_number"] != NULL) {

	$username = $_COOKIE["stu_number"];

	echo "<div class='stu_text'><span class='login'>欢迎你，<span class='color'>{$username}</span>同学</span>";
	echo "&nbsp;&nbsp;&nbsp;&nbsp;";
	echo "<a href=\"logout.php?action=logout\" class='color logout'>退出登录</a></div>";
}
?>
			<p>这位同学很懒，木有签名的说~</p>
		</div>
		<div class="header-news">
			<a href="" class="news_a">
				<?php echo "<img src='images/news.png' alt='图片' class='news'>";?>
			</a>
		</div>
	</div>
</div>

<!-- 中间内容 -->
<div class="content clearfloat">
	<!-- 左边内容 -->
	<div class="left_content">
		<ul class="left_nav" id="left_ul">
			<li><a href="notice.php" class="li_a">
				<span class="text">
					<?php echo "<img src='images/notice.png' alt='图片' class='left_img left_img1' id='left_img1'";?>
					<?php //echo "<img src='images/notice_style.png' alt='图片' class='left_img left_im' id='left_img1'";?>
					<span>动&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;态</span>
				</span>
				<canvas id="canvas_index1" width="18" height="48"></canvas>	
			</a></li>
			<li><a href="addcourse.php" class="li_a">
				<span class="text">
					<?php echo "<img src='images/course.png' alt='图片' class='left_img2' id='left_img2'";?>
					<span>我的作业</span>
				</span>
				<canvas id="canvas_index2" width="18" height="48"></canvas>
			</a></li>
			<li><a href="mycourse.php" class="li_a">
				<span class="text">
					<?php echo "<img src='images/mycourse.png' alt='图片' class='left_img3' id='left_img3'";?>
					<span>我的课程</span>
				</span>
				<canvas id="canvas_index3" width="18" height="48"></canvas>
			</a></li>
			<li class="active_nav"><a href="per_setup.php" class="li_a">
				<span class="text">
					<?php echo "<img src='images/setup.png' alt='图片' class='left_img4' id='left_img4'";?>
					<span>个人设置</span>
				</span>
				<canvas id="canvas_index4" width="18" height="48"></canvas>
			</a></li>
			<li><a href="" class="li_a">
				<span class="text">
					<?php echo "<img src='images/help.png' alt='图片' class='left_img5' id='left_img5'";?>
					<span>帮&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;助</span>
				</span>
				<canvas id="canvas_index5" width="18" height="48"></canvas>
			</a></li>
		</ul>
	</div>


	<!-- 右边内容 -->
	<div class="center_content">
		<a href="per_setup.php" class="per">返回</a>
		<form action="" method="post" enctype="multipart/form-data" class="per_infor">
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label for="stu_name">昵称:</label>
<?php 	
	echo "<input type='text' name='stu_name' id='stu_name' value='{$stu_name}'><br/>";
?>			
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label for="school">学校:</label>
<?php 
	echo "<input type='text' name='school' id='school' value='{$school}'><br/>";
?>
			<div class="touxiang">
				<div id="image-holder">
<?php
$file_img = "images/touxiang.jpg";

if(isset($_COOKIE["stu_number"])) {

	$username = $_COOKIE["stu_number"];

	$selectstu = "select stu_touxiang from students where stu_number='$username'";
	$result = mysql_query($selectstu) or die("查询学生信息表头像失败：".mysql_error()."<br/>");

	$row = mysql_fetch_array($result,MYSQL_ASSOC);

	$file_img = $row["stu_touxiang"];

	if($file_img == NULL) {
		$file_img = "images/touxiang.jpg";
	} else {
		$file_img = $row["stu_touxiang"];
	}
}
?>
					<?php echo "<img src='{$file_img}' alt='图片' id='file_img'>";?>
				</div>
				<a href="javascript:void(0);" class="input" id="huan">
					换一换<input type="file" id="upfile" name="upfile" value="换一换">
				</a>&nbsp;&nbsp;&nbsp;&nbsp;
				<a href="javascript:void(0);" class="input">
					上传头像<input type="file" id="infile" name="infile">
				</a>
			</div>
			
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label for="stu_phone">电话:</label>
<?php
	echo "<input type='text' name='stu_phone' id='stu_phone' value='{$stu_phone}'>";
?>
			<label>所在地区:</label>
<?php
	echo "<select name='stu_province' id='province'>
			<option value='省' selected='selected'>省</option>
			<option value='广西壮族自治区'>广西壮族自治区</option>
		</select>";
	echo <<<script_select
	<script>
		var province = document.getElementById("province");
		var option = province.children;
		var len = option.length;
		for(var i = 0; i < len; i++) {
			option[i].selected = "";

			if(option[i].innerHTML == '{$stu_province}') {
				option[i].selected = "selected";
			} else {
				option[0].selected = "selected";
			}
		}
	</script>
script_select;
?>
<?php
	echo "<select name='stu_city' id='city'>
			<option value='市' selected='selected'>市</option>
			<option value='南宁市'>南宁市</option>
			<option value='桂林市'>桂林市</option>
			<option value='柳州市'>柳州市</option>
			<option value='百色市'>百色市</option>
			<option value='崇左市'>崇左市</option>
		</select>";
	echo <<<script_select
	<script>
		var city = document.getElementById("city");
		var option = city.children;
		var len = option.length;
		for(var i = 0; i < len; i++) {
			option[i].selected = "";

			if(option[i].innerHTML == '{$stu_city}') {
				option[i].selected = "selected";
			} else {
				option[0].selected = "selected";
			}
		}
	</script>
script_select;
?>
<?php
	echo "<select name='stu_county' id='county'>
			<option value='区县' selected='selected'>区县</option>
			<option value='阳朔县'>阳朔县</option>
			<option value='临桂县'>临桂县</option>
		</select><br/>";
	echo <<<script_select
	<script>
		var county = document.getElementById("county");
		var option = county.children;
		var len = option.length;
		for(var i = 0; i < len; i++) {
			option[i].selected = "";

			if(option[i].innerHTML == '{$stu_county}') {
				option[i].selected = "selected";
			} else {
				option[0].selected = "selected";
			}
		}
	</script>
script_select;
?>

			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label for="stu_email">邮箱:</label>
<?php
	echo "<input type='text' name='stu_email' id='stu_email' value='{$stu_email}'>";
?>
			<label for="name">真实姓名:</label>
<?php
	echo "<input type='text' name='name' id='name' value='{$name}'><br/>";
?>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label>性别:</label>
<?php 
	echo "<span id='sex'>
				<input type='radio' name='stu_sex' value='男' checked='checked' class='sex'>&nbsp;&nbsp;男&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				<input type='radio' name='stu_sex' value='女' class='sex'>&nbsp;&nbsp;女<br/>
			</span>";
	echo <<<script_radio
	<script>
		var sex = document.getElementById("sex");
		var input = sex.children;
		var len = input.length;
		for(var i = 0; i < len; i++) {
			input[i].checked = "";

			if(input[i].value == '{$stu_sex}') {
				input[i].checked = "checked";
			} else {
				input[0].checked = "checked";
			}
		}
	</script>
script_radio;
?>
			<label for="stuper_signature">个性签名:</label>
<?php
	echo "<textarea name='stuper_signature' id='stuper_signature' cols='30' rows='10'>{$stuper_signature}</textarea><br/>";
?>
  			<input type="button" name="submit" value="确定" class="btn btn_left" onclick="window.open('per_setup.php','_self')">
  			<input type="reset" name="quxiao" value="取消" class="btn btn_right">
		</form>
	</div>

	<!-- 右侧栏 -->
	<?php
		include_once("right_content.php");
	?>
</div>

<!-- 加载左边导航的样式 -->
<?php
	include_once("nav_style.php");
?>

<!-- 加载底部信息 -->
<?php
	include_once("footer.php");
?>


<?php
if(isset($_COOKIE["stu_number"])) {
	$stu_number = $_COOKIE["stu_number"];

	if(isset($_POST["submit"]) && $_POST["submit"] == "确定") {
		foreach($_FILES as $key => $value) {
			foreach ($_FILES[$key] as $ke => $valu) {
			}
		}

		if(!empty($_FILES[$key]["name"])) {//判断是否选择了文件上传
			//指定上传文件路径以及名字
			$pathname = "./imgs/".$_FILES[$key]["name"];

			$stu_touxiang = "imgs/".$_FILES[$key]["name"];
		
			$result = move_uploaded_file($_FILES[$key]["tmp_name"], $pathname);					
		} else {

			$selectstu = "select stu_touxiang from students where stu_number='$stu_number'";
			$result = mysql_query($selectstu) or die("查询学生信息表头像失败：".mysql_error()."<br/>");

			$row = mysql_fetch_array($result,MYSQL_ASSOC);

			$stu_touxiang = $row["stu_touxiang"];

			if($stu_touxiang == "images/touxiang.jpg" || $stu_touxiang == "imgs/" || $stu_touxiang == "") {
				
				$stu_touxiang = "images/touxiang.jpg";
			} 
		}


		$stu_name = $_POST["stu_name"];
		$school = $_POST["school"];
		$stu_phone = $_POST["stu_phone"];
		$stu_email = $_POST["stu_email"];
		$stu_province = $_POST["stu_province"];
		$stu_city = $_POST["stu_city"];
		$stu_county = $_POST["stu_county"];
		$stu_sex = $_POST["stu_sex"];
		$name = $_POST["name"];
		$stuper_signature = $_POST["stuper_signature"];

		$updatesql = "update students set stu_name='$stu_name',school='$school',stu_phone='$stu_phone',
			stu_email='$stu_email',stu_province='$stu_province',stu_city='$stu_city',
			stu_county='$stu_county',stu_sex='$stu_sex',name='$name',
			stuper_signature='$stuper_signature',stu_touxiang='$stu_touxiang' where stu_number='$stu_number'";
		
		@mysql_query($updatesql) or die("修改学生信息表失败：".mysql_error()."<br/>");
	
		$selectstu = "select stu_touxiang from students where stu_number='$stu_number'";
		$result = mysql_query($selectstu) or die("查询学生信息表头像失败：".mysql_error()."<br/>");

		$row = mysql_fetch_array($result,MYSQL_ASSOC);

		$img = $row["stu_touxiang"];

		echo <<<script_img
		<script>
			var img_logo = document.getElementById("logo");
			var file_img = document.getElementById("file_img");

			img_logo.src = "{$img}";
			file_img.src = "{$img}"; 
		</script>
script_img;
	}
}
?>

<script src="js/jquery-3.2.1.js"></script>
<script>
    $("#upfile").on('change', function () {
        if (typeof (FileReader) != "undefined") {

            var image_holder = $("#image-holder");
            image_holder.empty();

            var reader = new FileReader();
            reader.onload = function (e) {
                $("<img />", {
                    "src": e.target.result,
                    "class": "thumb-image"
                }).appendTo(image_holder);
            };
            image_holder.show();
            reader.readAsDataURL($(this)[0].files[0]);
        } else {
            alert("你的浏览器不支持FileReader.");
        }

    });

    $("#infile").on('change', function () {
        if (typeof (FileReader) != "undefined") {

            var image_holder = $("#image-holder");
            image_holder.empty();

            var reader = new FileReader();
            reader.onload = function (e) {
                $("<img />", {
                    "src": e.target.result,
                    "class": "thumb-image"
                }).appendTo(image_holder);
            };
            image_holder.show();
            reader.readAsDataURL($(this)[0].files[0]);
        } else {
            alert("你的浏览器不支持FileReader.");
        }
    });
</script>
</body>
</html>